<template>
  <!-- 添加产品-->
  <el-dialog
    title="添加产品"
    v-model="visible"
    width="50%"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="addForm"
      label-width="auto"
      label-position="right"
    >
      <el-form-item label="产品名称" prop="name">
        <el-input
          v-model="form.name"
          clearable
          placeholder="请输入产品名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="产品图片" style="width: 100%">
        <sc-upload-multiple v-model="form.vendorUrlList" :limit="5">
        </sc-upload-multiple>
      </el-form-item>
      <el-form-item label="规格型号" prop="specification" style="width: 100%">
        <el-input
          v-model="form.specification"
          type="textarea"
          rows="3"
          clearable
          placeholder="请输入产品规格型号"
        ></el-input>
      </el-form-item>
      <el-form-item label="材质" style="width: 100%">
        <el-input
          v-model="form.material"
          type="textarea"
          rows="3"
          clearable
          placeholder="请输入产品规格型号"
        ></el-input>
      </el-form-item>
      <el-form-item label="产品单价（元）" class="priceItem">
        <el-input-number
          v-model="form.onePrice"
          :precision="2"
          :step="1"
          clearable
          placeholder="请输入产品单价"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-select
          v-model="form.unit"
          placeholder="请选择单位"
          style="width: 100%"
          remote
          filterable
          remote-show-suffix
          clearable
        >
          <el-option
            v-for="item in unitList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark" style="width: 100%">
        <el-input
          v-model="form.remark"
          rows="3"
          clearable
          type="textarea"
          placeholder="请输入备注"
        ></el-input>
      </el-form-item>
      <el-form-item label="是否是拆解件">
        <el-switch
          v-model="form.isDismantle"
          :inactive-value="0"
          :active-value="1"
        />
      </el-form-item>
      <el-form-item
        label="拆解件列表"
        style="width: 100%"
        v-show="form.isDismantle == 1"
      >
        <sc-form-table
          ref="innerTable"
          :modelValue="disassembleProductList"
          :addTemplate="productsTemplate"
          v-show="form.isDismantle == 1"
        >
          <el-table-column prop="name" label="拆解件名称" min-width="200">
            <template #default="scope">
              <el-input
                v-model="scope.row.name"
                placeholder="请输入拆解件名称"
              />
            </template>
          </el-table-column>
          <el-table-column prop="proportion" label="拆解比例" width="300">
            <template #default="scope">
              <el-input
                v-model="scope.row.proportion"
                placeholder="请输入拆解比例"
              />
            </template>
          </el-table-column>
          <el-table-column label="图片" min-width="300">
            <template #default="scope">
              <sc-upload-multiple v-model="scope.row.vendorUrlList" :limit="5">
              </sc-upload-multiple>
            </template>
          </el-table-column>
        </sc-form-table>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="save" :loading="isSaveing"
        >保 存</el-button
      >
      <el-button @click="visible = false">取 消</el-button>
    </template>
  </el-dialog>
</template>

<script>

import { queryTypeByDic } from '@/utils/getData'
export default {
  emits: ['getProductData'],
  data () {
    return {
      visible: false,
      isSaveing: false,
      selectVendorLoading: false,
      //表单数据
      form: {
        name: '',//产品名称
        specification: '',//规格型号
        material: '',//材质
        onePrice: 0,//单价
        unit: '',//单位
        remark: '',//产品备注
        isDismantle: 0,//是否拆解件 0否 1是
        productsDismantleList: [],//拆解件
        vendorUrlList: []//	产品图片
      },
      disassembleProductList: [
        {
          name: '',
          proportion: '',//比例
          vendorUrlList: []//图片

        }
      ],//拆解产品
      //拆解产品模版
      productsTemplate: {
        name: '',
        proportion: '',//比例
        vendorUrlList: []//图片
      },
      vendorList: [],
      unitList: [],
      //验证规则
      rules: {
        name: [
          { required: true, message: '请输入产品名称' }
        ],
        onePrice: [
          { required: true, message: '请输入产品价格' }
        ],
        unit: [
          { required: true, message: '请选择单位', trigger: 'change' }
        ],
      }

    }
  },

  mounted () {
    this.selectdictionaryBy()
  },
  methods: {
    //显示
    open (data) {
      this.visible = true
      console.log(data)
    },
    // 获取单位
    async selectdictionaryBy () {
      let result = await queryTypeByDic('unitCode')
      this.unitList = result
    },

    save () {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          this.form.productsDismantleList = this.disassembleProductList
          this.isSaveing = true
          let res = null
          let mode = this.$route.query.mode
          res = await this.$API.vendorProducts.addVendorProducts.post(this.form)
          this.isSaveing = false
          if (res.code == 200) {
            this.visible = false
            this.$message.success("操作成功")
            this.$emit('getProductData', res.data)
            // this.$router.push({
            //   path: "/customer/vendorProducts",
            // })
          }

        }
      })
    },
  }
}
</script>

<style lang='scss' scoped>
.button {
  margin-top: 50px;
  padding-left: 60px;
}

:deep(.el-divider__text) {
  padding: 0 10px;
  font-size: 18px;
  // font-weight: 600;
  background-color: #f6f8f9;
}
</style>
